#ifndef ILK_BACK_H
#define ILK_BACK_H

/*
Include:
#include <dirent.h>
#include <rid.h>
#include <stdint.h>

#include <unfy/unfy.h>
#include <rls/rls.h>
#include <rls/rlsp.h>

#include <nit/list.h>
#include <nit/set.h>
#include <nit/map.h>
#include <nit/err.h>
#include <nit/file.h>
#include <nit/buf.h>
#include <nit/gap.h>

#include <ilk/utilize.h>
*/

#ifndef STYLE_9
#include <rls/rlsp.h>
#include <nit/map.h>
#include <nit/file.h>
#include <nit/gap.h>
#endif

typedef struct {
	uint16_t max_num;
	Rls_act *table;
} Ilk_act_dat;

typedef struct {
	uint64_t srch_cnt;
	uint16_t act; /* for actable functions */
} Ilk_more;

typedef struct {
	Rid id;
	Rid grp_id;
	uint64_t srch_cnt;
	Nit_gap deps;
	unsigned changed : 1;
} Ilk_meta;

typedef struct {
	Rid id;
	Rls_rule *rules;
	unsigned changed : 1;
	unsigned accessed : 1;
} Ilk_grp;

typedef struct Ilk_sys Ilk_sys;

typedef struct {
	Ilk_act_dat act_dat;
	Rls_bend bend;
	Rlsp_srlz srlz;
	Ilk_grp *old_grps;
	Ilk_more *old_mores;
	Ilk_sys *old_syss;
	Rls_recycle rec;
	Nit_entry *stack;
	Nit_buf buf;
	Nit_err err;
	unsigned testing : 1;
} Ilk_lib;

struct Ilk_sys {
	Ilk_lib *lib;
	Nit_map grps;
	Nit_map srchs;
	Nit_dir dir;
	uint64_t srch_cnt;
};

extern const Rid ilk_add_id;
extern const Rid ilk_id_id;
extern const Rid ilk_head_id;

/* grp */

Ilk_grp *
ilk_grp_load(Ilk_sys *sys, const Rid id, Unfy_stat *stat);

int
ilk_grp_unload(Ilk_grp *grp, Ilk_sys *sys);

void
ilk_grp_discard(Ilk_grp *grp, Ilk_sys *sys);

Ilk_grp *
ilk_grp_get(Ilk_sys *sys, const Rid id, Unfy_stat *stat);

Ilk_grp *
ilk_grp_get_term(Ilk_sys *sys, const Unfy_term *term, Unfy_stat *stat);

int
ilk_grp_save(Ilk_grp *grp, Ilk_sys *sys);

/* rule */

int
ilk_rule_origin(Rls_rule *rule, Rls_srch **srch_ref,
		uint64_t srch_cnt, Ilk_sys *sys);

/* meta */

int
ilk_meta_init(Ilk_meta *meta, uint64_t srch_cnt, uint64_t deps_len);

void
ilk_meta_dispose(Ilk_meta *meta);

void
ilk_meta_get(Ilk_meta *meta, Rls_srch **srch_ref, Ilk_sys *sys);

int
ilk_meta_save(Ilk_sys *sys, Ilk_meta *meta);

int
ilk_meta_load(Ilk_meta *meta, const Rid id, Ilk_sys *sys);

int
ilk_meta_dep(Ilk_meta *meta, uint64_t srch_cnt);

/* srch */

Unfy_stat
ilk_srch(Ilk_sys *sys, Rls_srch **srch_ref, Unfy_term *term,
	 int *limit, Rls_info *info);

int
ilk_srch_get(Rls_srch **srch_ref, uint64_t srch_cnt, Ilk_sys *sys);

int
ilk_srch_save(Ilk_sys *sys, Rls_srch *srch, uint64_t srch_cnt);

int
ilk_srch_load(Rls_srch **srch, uint64_t srch_cnt, Ilk_sys *sys);

int
ilk_srch_unload(uint64_t srch_cnt, Ilk_sys *sys);

#endif
